﻿using UnityEngine;
using System.Collections;

public class DirectionsScreenScript : MonoBehaviour {

	SpriteRenderer birdLeft;
	SpriteRenderer birdRight;
	SpriteRenderer panelLeft;
	SpriteRenderer panelRight;
	SpriteRenderer thumbs;
	TextMesh debugText;
	TextMesh instructionsText;

	Animator leftAnim;
	Animator rightAnim;

	float leftStart = 2f;
	float rightStart = 2f;
	float leftTimer = 2f;
	float rightTimer = 2f;

	bool leftSide = true;

	Animator arrowAnim;
	SpriteRenderer arrowRend;
	
	bool clickEvent = false;
	bool alreadyClicked = false;
	bool clickDown = false;
	bool clickUp = false;
	
	Vector2 pos = Vector2.zero;


	// Use this for initialization
	void Start () {
		this.birdLeft = (SpriteRenderer)GameObject.Find ("BirdLeft").GetComponent (typeof(SpriteRenderer));
		this.birdRight = (SpriteRenderer)GameObject.Find ("BirdRight").GetComponent (typeof(SpriteRenderer));
		this.panelLeft = (SpriteRenderer)GameObject.Find ("LeftPanel").GetComponent (typeof(SpriteRenderer));
		this.panelRight = (SpriteRenderer)GameObject.Find ("RightPanel").GetComponent (typeof(SpriteRenderer));
		this.thumbs = (SpriteRenderer)GameObject.Find ("Thumbs").GetComponent (typeof(SpriteRenderer));

		//this.debugText = (TextMesh)GameObject.Find ("DebugText").GetComponent (typeof(TextMesh));
		this.instructionsText = (TextMesh)GameObject.Find ("InstructionsText").GetComponent (typeof(TextMesh));

		this.leftAnim = (Animator)this.birdLeft.gameObject.GetComponent (typeof(Animator));
		this.rightAnim = (Animator)this.birdRight.gameObject.GetComponent (typeof(Animator));

		this.leftAnim.SetBool ("IsFlapping", true);

		this.arrowAnim = (Animator)GameObject.Find ("Arrow").GetComponent (typeof(Animator));
		this.arrowRend = (SpriteRenderer)GameObject.Find("Arrow").GetComponent (typeof(SpriteRenderer));
		
		//this.questionAnim.SetTrigger ("QuestionUp");
		//this.arrowAnim.SetTrigger ("ArrowUp");
		
		this.arrowAnim.SetInteger ("ArrowInt", 1);


		GameMaintainerSingleton.Instance.SetUsed();
	}
	
	// Update is called once per frame
	void Update () {
		//leftTimer -= Time.deltaTime;
		//rightTimer -= Time.deltaTime;
		if (Input.GetKey (KeyCode.Escape)) {
			GameMaintainerSingleton.Instance.ShowAds();
			Application.LoadLevel(0);
		}

		if (leftSide) {
			leftTimer -= Time.deltaTime;
			if (leftTimer <= 0f) {
				Color col = panelLeft.color;
				col.a = 0f;
				panelLeft.color = col;
				leftAnim.SetBool("IsFlapping",false);
				rightAnim.SetBool("IsFlapping",true);
				rightTimer = rightStart;
				leftSide = false;

				this.instructionsText.text = "Tap the Right Side of the Screen to Fly Right";
			}
			else {
				float alpha = Mathf.Lerp(0f,0.5f,leftTimer / leftStart);
				//Debug.Log("LEFT " + alpha);
				Color col = panelLeft.color;
				col.a = alpha;
				panelLeft.color = col;
			}
		}
		else {
			rightTimer -= Time.deltaTime;
			if (rightTimer <= 0f) {
				Color col = panelRight.color;
				col.a = 0f;
				panelRight.color = col;
				leftAnim.SetBool("IsFlapping",true);
				rightAnim.SetBool("IsFlapping",false);
				leftTimer = leftStart;
				leftSide = true;

				this.instructionsText.text = "Tap the Left Side of the Screen to Fly Left";
			}
			else {
				float alpha = Mathf.Lerp(0f,0.5f,rightTimer / rightStart);
				//Debug.Log ("RIGHT " + alpha);
				Color col = panelRight.color;
				col.a = alpha;
				panelRight.color = col;
			}
		}




		clickEvent = false;
		clickDown = false;
		clickUp = false;
		if (Input.GetMouseButtonDown (0)) {
			pos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
			//Debug.Log("MOUSE DOWN");
			clickEvent = true;
			if (!alreadyClicked) { 
				clickDown = true;
				alreadyClicked = true;
			}
			else { 
				clickDown = true;
				alreadyClicked = false;
			}
		} else if (Input.GetMouseButtonUp (0)) {
			pos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
			//Debug.Log("MOUSE UP");
			clickDown = false;
			alreadyClicked = false;
			clickUp = true;
			//alreadyClicked = false;
		} else if (Input.touchCount > 0) {
			//Debug.Log ("TOUCH");
			Touch letouch = Input.GetTouch(0);
			pos = letouch.position;
			if (letouch.phase == TouchPhase.Began) {
				clickEvent = true;
				//Debug.Log("TOUCH BEGAN");
				if (!alreadyClicked) { 
					clickDown = true;
					alreadyClicked = true;
				}
				else { 
					clickDown = true;
					alreadyClicked = false;
				}
			}
			else if (letouch.phase == TouchPhase.Ended || letouch.phase == TouchPhase.Canceled) {
				//Debug.Log("TOUCH ENDED");
				clickDown = false;
				alreadyClicked = false;
				clickUp = true;
			}
		}
		
		if (clickDown) {
			//Debug.Log("PROC 1");
			//this.debugText.text = pos.ToString ();
			
			//if (this.arrow.bounds.Contains(new Vector3(pos.x,pos.y,0f))) {
			//this.debugText.text = "ARROW";
			//}
			
			Vector3 arrowMin = Camera.main.WorldToScreenPoint(this.arrowRend.bounds.min);
			Vector3 arrowMax = Camera.main.WorldToScreenPoint(this.arrowRend.bounds.max);
			
			bool inArrow = false;
			bool inQuestion = false;
			if (pos.x >= arrowMin.x && pos.x <= arrowMax.x && pos.y >= arrowMin.y && pos.y <= arrowMax.y) {
				//Application.LoadLevel(1);
				inArrow = true;
			}

			if (inArrow) {
				this.arrowAnim.SetInteger ("ArrowInt", 0);
			}
			else {
				this.arrowAnim.SetInteger ("ArrowInt", 1);
			}
			//this.debugText.text = arrowMin.ToString() + " - " + arrowMax.ToString();
		}
		else if (clickUp) {
			Vector3 arrowMin = Camera.main.WorldToScreenPoint(this.arrowRend.bounds.min);
			Vector3 arrowMax = Camera.main.WorldToScreenPoint(this.arrowRend.bounds.max);
			
			bool inArrow = false;
			if (pos.x >= arrowMin.x && pos.x <= arrowMax.x && pos.y >= arrowMin.y && pos.y <= arrowMax.y) {
				//Application.LoadLevel(1);
				inArrow = true;
			}
			if (inArrow) {
				Application.LoadLevel(1);
			}
			else {
				this.arrowAnim.SetInteger ("ArrowInt", 1);
			}
		}

		
	}
}
